<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="咨询用户" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入咨询用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="问题类型" prop="typeName">-->
<!--        <el-input-->
<!--          v-model="queryParams.typeName"-->
<!--          placeholder="请输入问题类型"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="问题标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入问题标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="经度" prop="longitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.longitude"-->
<!--          placeholder="请输入经度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="纬度" prop="latitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.latitude"-->
<!--          placeholder="请输入纬度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="具体地址" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入具体地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:dispute:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:dispute:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:dispute:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:dispute:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="disputeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" v-if="false"/>
      <el-table-column label="咨询用户" align="center" prop="createBy" />
<!--      <el-table-column label="问题类型" align="center" prop="typeName" />-->
      <el-table-column label="问题标题" align="center" prop="title"   show-overflow-tooltip/>
      <el-table-column label="问题图片" align="center" prop="imgUrl" width="200">
        <template scope="scope">
          　　　　<img v-if="scope.row.imgUrl === null || scope.row.imgUrl=== ''"
                   src="https://callmejord.top:9119/profile/upload/2021/06/07/2e0331bc-bd2e-4e61-9969-a39b81aa7733.png"
                   width="200" height="200"
                    class="head_pic"/>
          <img v-else :src="scope.row.imgUrl" width="200" height="200"
                class="head_pic"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="description"
                       show-overflow-tooltip width="300"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="指派" align="center" prop="distributionUser" />

      <el-table-column label="回复" align="center" prop="longitude" width="300"/>
<!--      <el-table-column label="纬度" align="center" prop="latitude" />-->
      <el-table-column label="具体地址" align="center" prop="location" width="300"/>
      <el-table-column label="备注" align="center" prop="remark" width="300"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:dispute:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:dispute:remove']"
          >删除</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:dispute:edit']"-->
<!--          >指派</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改纠纷信息列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="咨询用户" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入咨询用户" />
        </el-form-item>
<!--        <el-form-item label="问题类型" prop="typeName">-->
<!--          <el-input v-model="form.typeName" placeholder="请输入问题类型" />-->
<!--        </el-form-item>-->
        <el-form-item label="问题标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="问题描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入问题描述" type="textarea"
                    :rows="5"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="form.status"
            placeholder="状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="指派">
          <el-select
            v-model="form.distributionUserId"
            placeholder="指派"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="item in personOptions"
              :key="item.userName"
              :label="item.userName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="经度" prop="longitude">-->
<!--          <el-input v-model="form.longitude" placeholder="请输入经度" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="纬度" prop="latitude">-->
<!--          <el-input v-model="form.latitude" placeholder="请输入纬度" />-->
<!--        </el-form-item>-->
        <el-form-item label="具体地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入具体地址" />
        </el-form-item>
<!--        <el-form-item label="删除标志" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDispute, getDispute, delDispute, addDispute, updateDispute,
  exportDispute, getJFPersonList } from "@/api/business/dispute";
let el;
let tableBodyWrapDom;
let tableBodyDom;

function handle() {
  if (!el) return;
  // top为dom上侧距离可视窗口顶部的值
  let { top: tableBodyDomTop } = tableBodyWrapDom.getBoundingClientRect();  
  //console.log("aaaaaaaaaaaaaaaaaa="+tableBodyDomTop);
  if (tableBodyDomTop > window.innerHeight || tableBodyWrapDom.classList.contains('is-scrolling-none')) {
    // 此时列表在可视窗口的下侧不可见区域，因此不做任何修改
    tableBodyWrapDom.style.height = 'unset'
    tableBodyWrapDom.style.marginBottom = 'unset';
  } else {
    // 窗口高度 - 列表距顶部值 且 不超过自身实际值
    let wrapHeight = Math.min(window.innerHeight - tableBodyDomTop, tableBodyDom.offsetHeight);
    //console.log("bbbbbbbbbbbbbbb="+wrapHeight+"+"+tableBodyDom.offsetHeight);
    tableBodyWrapDom.style.height = wrapHeight + 'px';
    // 需要用marginBottom填充，以保持列表原有高度，避免页面的纵向滚动条变化导致页面滚动的不流畅
    // 可以通过注释这一行代码观察其效果差异
    tableBodyWrapDom.style.marginBottom = (tableBodyDom.offsetHeight - wrapHeight) + 'px';
  }
}
export default {
  name: "Dispute",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 纠纷信息列表格数据
      disputeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      statusOptions: [],
      personOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        typeName: undefined,
        title: undefined,
        imgUrl: undefined,
        description: undefined,
        status: undefined,
        longitude: undefined,
        latitude: undefined,
        location: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_dispute_status").then(response => {
      this.statusOptions = response.data;
    });
    this.listPerson();
  },
  mounted(){//console.log("mounted...............")
    el = this.$el; // 当前组件的Dom对象，避免操作到本组件之外的Dom
    tableBodyWrapDom = el.querySelector('.el-table__body-wrapper');
    tableBodyDom = el.querySelector('.el-table__body');
    // 监听事件
    document.addEventListener('scroll', handle,true);
    window.addEventListener('resize', handle,true);

  },destroyed() {//console.log("destroyed...............")
  	// 在组件销毁时取消监听
    document.removeEventListener('scroll', handle,true);
    window.removeEventListener('resize', handle,true);
  },
  activated() {//console.log("activated...............")
  el = this.$el; // 当前组件的Dom对象，避免操作到本组件之外的Dom
    tableBodyWrapDom = el.querySelector('.el-table__body-wrapper');
    tableBodyDom = el.querySelector('.el-table__body');
    // 全局绑定滚动事件，
    document.addEventListener('scroll', handle,true);
    window.addEventListener('resize', handle,true);
  },
  deactivated() {//console.log("deactivated...............")
    // 组件消失，解绑scroll事件
    document.removeEventListener('scroll', handle,true);
    window.removeEventListener('resize', handle,true);
  },
  methods: {
    listPerson() {
      getJFPersonList().then(response => {
        this.personOptions = response.data;
      });
    },
    /** 查询纠纷信息列列表 */
    getList() {
      this.loading = true;
      listDispute(this.queryParams).then(response => {
        this.disputeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    statusFormat(row, column) {
      if (row.menuType == "F") {
        return "";
      }
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        typeName: undefined,
        title: undefined,
        imgUrl: undefined,
        description: undefined,
        status: "1",
        longitude: undefined,
        latitude: undefined,
        location: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        distributionUserId: undefined,
        distributionUser: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加纠纷信息列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDispute(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改纠纷信息列";
      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDispute(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改纠纷信息列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDispute(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDispute(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除纠纷信息列编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDispute(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有纠纷信息列数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDispute(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
